package xj.graph2d.dialog;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.LayoutManager;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import xj.util.gui.DialogLayout;

public class DialogPanel extends JPanel {

  public DialogPanel(String name) {
    this(name, null, false);
  }

  public DialogPanel(String name, String title) {
    this(name, title, false);
  }

  public DialogPanel(String name, boolean stretchLastRow) {
    this(name, null, stretchLastRow);
  }

  public DialogPanel(String name, String title, boolean stretchLastRow) {
    this.name = name;
    if (stretchLastRow) { 
      setLayout(new DialogLayout(DialogLayout.STRETCH_CENTER));
    } else { 
      setLayout(new DialogLayout());
    }
    if (title != null) { 
      /*
      setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title, 
						 TitledBorder.LEFT, TitledBorder.BELOW_TOP,
						 titleFont, Color.BLACK));
      */
      setBorder(BorderFactory.createEtchedBorder());      
    } else { 
      setBorder(BorderFactory.createEtchedBorder());
    }
  }

  public DialogPanel(String name, LayoutManager layout) {
    this.name = name;
    setLayout(layout);
    setBorder(BorderFactory.createEtchedBorder());
  }

  public void addRow(Component left, Component center) {
    add(left, DialogLayout.LEFT);
    add(center, DialogLayout.CENTER);
  }

  public void addRow(Component left, Component center, Component right) {
    add(left, DialogLayout.LEFT);
    add(center, DialogLayout.CENTER);
    add(right, DialogLayout.RIGHT);
  }

  public void addRow(Component center) {
    add(center, DialogLayout.CENTER);
  }

  @Override
  public String getName() {
    return name;
  }

  public void setListener(ChangeListener listener) {
    this.listener = listener;
  }
  
  public void setOption() {
  }

  public void applyOption() {
    if (listener != null) {
      listener.stateChanged(new ChangeEvent(this));
    }
  }

  @Override
  public void setName(String name) { 
    this.name = name;
    //setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), name));
  }
  
  @Override
  public Dimension getPreferredSize() {
    return dim;
  }

  @Override
  public Dimension getMinimumSize() {
    return minDim;
  }

  @Override
  public void setPreferredSize(Dimension dim) {
    this.dim = dim;
  }

  @Override
  public void setMinimumSize(Dimension minDim) {
    this.minDim = minDim;
  }

  protected String name;

  protected ChangeListener listener;

  protected Dimension dim = defaultDim;

  protected Dimension minDim = defaultMinDim;

  private static Dimension defaultDim = new Dimension(300, 200);

  private static Dimension defaultMinDim = new Dimension(200, 100);

}
